package com.umeox.watch.moto.settings.ui;


import static com.umeox.watch.moto.settings.Constant.Units.KEY_UNIT_ARRAY_ID;
import static com.umeox.watch.moto.settings.Constant.Units.KEY_UNIT_RESULT;
import static com.umeox.watch.moto.settings.Constant.Units.KEY_UNIT_SELETED;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.settings.R;

import java.util.Arrays;
import java.util.List;

public class UnitOptionActivity extends ThemeAppCompatActivity implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private TextView mUnitRb_1;
    private TextView mUnitRb_2;

    private String[] mUnitArray;

    private String mOptionSelected;
    private List<String> mUnitList;
    private ImageView ivDone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_unit_option);
        Intent intent = getIntent();
        int units = intent.getIntExtra(KEY_UNIT_ARRAY_ID, R.array.Unit_Distance);
        mOptionSelected = intent.getStringExtra(KEY_UNIT_SELETED);
        mUnitArray = getResources().getStringArray(units);
        initView();

    }

    private void initView() {

        mUnitRb_1 = findViewById(R.id.rb_unit_1);
        mUnitRb_2 = findViewById(R.id.rb_unit_2);

        ivDone = findViewById(R.id.iv_done);

        mUnitRb_1.setText(mUnitArray[0]);
        mUnitRb_2.setText(mUnitArray[1]);

        mUnitRb_1.setOnClickListener(this);
        mUnitRb_2.setOnClickListener(this);


        mUnitList = Arrays.asList(mUnitArray);
        mUnitRb_1.setSelected(mUnitList.indexOf(mOptionSelected) == 0);
        mUnitRb_2.setSelected(mUnitList.indexOf(mOptionSelected) == 1);


        ivDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data = new Intent();
                data.putExtra(KEY_UNIT_RESULT, Math.max(0, mUnitList.indexOf(mOptionSelected)));
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        TextView textView = (TextView) v;
        mUnitRb_1.setSelected(mUnitRb_1 == textView);
        mUnitRb_2.setSelected(mUnitRb_2 == textView);

        mOptionSelected = textView.getText().toString();
    }

}
